package com.xxkj.orcfx;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Properties;

public class UserSettings {

    private static final String SETTINGS_FILE = "user_settings.properties";
    private Properties properties;

    public final static  String SOURCE_FILE_DIR_NAME = "sourceFileDir";//原始文件的目录
    public final static  String DEST_FILE_DIR_NAME = "destFileDir";//输出文件目录
    public final static  String SELECTED_MODE_OPTION = "selectedModeOption";//选择的模式
    public final static  String COOKIE_SESSIONID = "cookie.wzws_sessionid";//
    public final static  String COOKIE_CID = "cookie.wzws_cid";//
    public final static  String COOKIE_JSESSIONID = "cookie.JSESSIONID";//


    public UserSettings() {
        properties = new Properties();
        loadProperties();
    }

    public void saveCookieProperties(String cookieCid, String cookieSessionId, String jSessionId) {
        if(StringUtils.isNotBlank(cookieCid)){
            properties.setProperty(COOKIE_CID, cookieCid);
        }
        if(StringUtils.isNotBlank(cookieSessionId)){
            properties.setProperty(COOKIE_SESSIONID, cookieSessionId);
        }

        if(StringUtils.isNotBlank(jSessionId)){
            properties.setProperty(COOKIE_JSESSIONID, jSessionId);
        }

        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(SETTINGS_FILE), "UTF-8")) {
            properties.store(writer, "User Settings");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public String getCookieCid() {
        return properties.getProperty(COOKIE_CID, "");
    }

    public String getCookieSessionId() {
        return properties.getProperty(COOKIE_SESSIONID, "");
    }

    public String getJSessionId() {
        return properties.getProperty(COOKIE_JSESSIONID, "");
    }


    public void saveProperties(String sourceFileDir, String destFileDir, String selectedModeOption) {
        properties.setProperty(SOURCE_FILE_DIR_NAME, sourceFileDir);
        properties.setProperty(DEST_FILE_DIR_NAME, destFileDir);
        properties.setProperty(SELECTED_MODE_OPTION, selectedModeOption);
        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(SETTINGS_FILE), "UTF-8")) {
            properties.store(writer, "User Settings");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getSourceFileDir() {
        return properties.getProperty(SOURCE_FILE_DIR_NAME, "");
    }

    public String getDestFileDir() {
        return properties.getProperty(DEST_FILE_DIR_NAME, "");
    }

    public String getSelectedModeOption() {
        return properties.getProperty(SELECTED_MODE_OPTION, "解析影像资料");
    }



    private void loadProperties() {
        try (InputStreamReader reader = new InputStreamReader(new FileInputStream(SETTINGS_FILE), "UTF-8")) {
            properties.load(reader);
        } catch (IOException e) {
            // 文件不存在，首次运行
            System.out.println("Properties file not found, creating new one.");
        }
    }

    public static void main(String[] args) {
        UserSettings userSettings = new UserSettings();

        // 保存设置
        userSettings.saveProperties("C:/path/to/file1.txt", "C:/path/to/file1.txt","选项11111");

        // 读取设置
        String filePath = userSettings.getSourceFileDir();
        String selectedOption = userSettings.getSelectedModeOption();

        System.out.println("File Path: " + filePath);
        System.out.println("Selected Option: " + selectedOption);
    }
}
